package com.jinzhi.jzweb.service.infoCollect.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jinzhi.common.utils.ShiroUtils;
import com.jinzhi.jzdoor.certificate.pojo.TrainingBaseVo;
import com.jinzhi.jzweb.dao.infoCollect.ActivityDao;
import com.jinzhi.jzweb.domain.infoCollect.ActivityInfoCollectEntity;
import com.jinzhi.jzweb.service.infoCollect.ActivityService;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import io.jsonwebtoken.lang.Objects;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityDao, ActivityInfoCollectEntity> implements ActivityService {

    @Override
    public Page<ActivityInfoCollectEntity> getPage(Integer pageNum, Integer pageSize) {
        if (pageNum == null || pageSize == null){
            pageNum =1;
            pageSize =10;
        }
        Page<ActivityInfoCollectEntity> page = new Page<>(pageNum, pageSize);
        Wrapper<ActivityInfoCollectEntity> activityInfoCollectEntityWrapper = new EntityWrapper<ActivityInfoCollectEntity>()
//                .like("associates", )
//                .like("unit_name", )
                .eq("is_delete", 0)
                .orderBy("create_date", false);

        Page<ActivityInfoCollectEntity> activityInfoCollectEntityPage = this.selectPage(page, activityInfoCollectEntityWrapper);
        List<ActivityInfoCollectEntity> collect = activityInfoCollectEntityPage.getRecords().stream().map(activityInfoCollectEntity -> {
            ActivityInfoCollectEntity entity = new ActivityInfoCollectEntity();
            BeanUtils.copyProperties(activityInfoCollectEntity, entity);
            entity.setNeeds(null);
            String needs = activityInfoCollectEntity.getNeeds();
            String[] split = needs.split(",");
            for (String s : split) {
                String need;
                switch (s) {
                    case "1":
                        need = "专业建设";
                        break;
                    case "2":
                        need = "师资培训";
                        break;
                    case "3":
                        need = "教材";
                        break;
                    case "4":
                        need = "实训室建设";
                        break;
                    case "5":
                        need = "实习就业";
                        break;
                    case "6":
                        need = "创新创业";
                        break;
                    default:
                        need = "";
                }
                if (entity.getNeeds() == "" || entity.getNeeds() == null) {
                    entity.setNeeds(need);
                } else {
                    entity.setNeeds(entity.getNeeds() + "，" + need);
                }
            }
            return entity;
        }).collect(Collectors.toList());
        page.setRecords(collect);

        return activityInfoCollectEntityPage;
    }

    @Override
    public Object addActivityInfo(ActivityInfoCollectEntity activityInfoCollectEntity) {
        Assert.notNull(activityInfoCollectEntity.getNeeds(), "用户需求不能为空！");
        Assert.notNull(activityInfoCollectEntity.getUnitName(), "单位/学校全称不能为空！");
        Assert.notNull(activityInfoCollectEntity.getAssociates(), "联系人不能为空！");
        Assert.notNull(activityInfoCollectEntity.getPhoneNumber(), "联系电话不能为空！");

        activityInfoCollectEntity.setCreateDate(String.valueOf(LocalDate.now()));
        activityInfoCollectEntity.setIsDelete("0");

        int insert = this.baseMapper.insert(activityInfoCollectEntity);
        return Optional.ofNullable(insert).isPresent();
    }

    @Override
    public Object updateInfo(ActivityInfoCollectEntity activityInfoCollectEntity) {
        Assert.notNull(activityInfoCollectEntity.getId(), "被删除信息id不能为空！");
        Assert.notNull(activityInfoCollectEntity.getNeeds(), "用户需求不能为空！");
        Assert.notNull(activityInfoCollectEntity.getUnitName(), "单位/学校全称不能为空！");
        Assert.notNull(activityInfoCollectEntity.getAssociates(), "联系人不能为空！");
        Assert.notNull(activityInfoCollectEntity.getPhoneNumber(), "联系电话不能为空！");

        activityInfoCollectEntity.setUpdateBy(ShiroUtils.getUserId().toString());
        activityInfoCollectEntity.setUpdateDate(String.valueOf(LocalDate.now()));

        int update = this.baseMapper.updateById(activityInfoCollectEntity);
        return Optional.ofNullable(update).isPresent();
    }

    @Override
    public Object deleteInfoById(String id) {
        Assert.notNull(id, "被删除信息id不能为空！");

        ActivityInfoCollectEntity activityInfoCollectEntity = new ActivityInfoCollectEntity();
        activityInfoCollectEntity.setId(id);
        activityInfoCollectEntity.setIsDelete("1");
        activityInfoCollectEntity.setUpdateDate(String.valueOf(LocalDate.now()));
        activityInfoCollectEntity.setUpdateBy(String.valueOf(ShiroUtils.getUserId()));

        int delete = this.baseMapper.updateById(activityInfoCollectEntity);
        return Optional.ofNullable(delete).isPresent();
    }

    @Override
    public ActivityInfoCollectEntity selectById(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public void exportInfo(HttpServletResponse response) {
        List<ActivityInfoCollectEntity> list = this.selectList(new EntityWrapper<ActivityInfoCollectEntity>().eq("is_delete", 0));
//        List<ActivityInfoCollectEntity> collect = list.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        list.stream().forEach(a -> {
            String[] split = a.getNeeds().split(",");
            a.setNeeds("");
            for (String s : split) {
                String need;
                switch (s) {
                    case "1":
                        need = "专业建设";
                        break;
                    case "2":
                        need = "师资培训";
                        break;
                    case "3":
                        need = "教材";
                        break;
                    case "4":
                        need = "实训室建设";
                        break;
                    case "5":
                        need = "实习就业";
                        break;
                    case "6":
                        need = "创新创业";
                        break;
                    default:
                        need = "";
                }
                if (a.getNeeds() == "" || a.getNeeds() == null) {
                    a.setNeeds(need);
                } else {
                    a.setNeeds(a.getNeeds() + "，" + need);
                }
            }
        });
        EasyPoiUtil.exportExcel(list, "产教融合活动采集数据", "产教融合活动采集数据", ActivityInfoCollectEntity.class,
                "产教融合活动采集数据", response);
    }


}
